<nz-card>
  <nz-descriptions
    nzTitle="基本信息"
    nzBordered
    nzSize="small"
    [nzColumn]="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }"
  >
    <nz-descriptions-item nzTitle="单号">
      {{ detailsData.deliveryNumber || '-' }}
      <i
        *ngIf="detailsData.deliveryNumber"
        nz-icon
        nzType="copy"
        nzTheme="outline"
        nz-tooltip
        nzTooltipTitle="点击复制"
        (click)="coprText(detailsData.deliveryNumber)"
      ></i>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="状态">
      <span [ngStyle]="{'color': detailsData.state | storeOutStatus: 'color'}">
        {{ detailsData.state | storeOutStatus: 'label' }}
      </span>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="出库仓库">
      {{ filterText(warehouseOptions, detailsData.whId, 'id') }}
      <button
        nz-button
        nzType="link"
        nzSize="small"
        [disabled]="!(permission.userPermission.has('storageCenter:out:examine') && [2, 3].includes(detailsData.state))"
        (click)="showEditStoreModal()"
      >修改</button>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="新建人">{{ detailsData.founder || '-' }}</nz-descriptions-item>

    <nz-descriptions-item nzTitle="关联单号">
      {{ detailsData.relationNum || '-' }}
      <i
        *ngIf="detailsData.relationNum"
        nz-icon
        nzType="copy"
        nzTheme="outline"
        nz-tooltip
        nzTooltipTitle="点击复制"
        (click)="coprText(detailsData.relationNum)"
      ></i>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="出库类型">
      {{ filterText(outTypeOptions, detailsData.type, 'content') }}
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="出库品质">
      <span>
        {{ detailsData.quality === 0 ? '不良品' : (detailsData.quality === 1 ? '良品' : '-') }}
      </span>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="新建时间">{{ detailsData.createTime || '-' }}</nz-descriptions-item>
    <nz-descriptions-item nzTitle="供应商">
      {{ filterText(supplierOptions, detailsData.supId, 'id') }}
    </nz-descriptions-item>

    <nz-descriptions-item nzTitle="预计到货日期">{{ detailsData.arrivalTime || '-' }}</nz-descriptions-item>
    <nz-descriptions-item nzTitle="出库时间">{{ detailsData.deliveryTime || '-' }}</nz-descriptions-item>
    <nz-descriptions-item nzTitle="签收时间">{{ detailsData.dealTime || '-' }}</nz-descriptions-item>

    <nz-descriptions-item nzTitle="附件" nzSpan="4">
      <ul>
        <li *ngFor="let item of detailsData?.fileList">
          {{ item.name }}
          <button
            nz-button
            nzType="link"
            nzSize="small"
            (click)="downFile(item)"
          >下载</button>
        </li>
      </ul>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="备注" nzSpan="4">{{ detailsData.remark || '-' }}</nz-descriptions-item>
  </nz-descriptions>

  <div class="p-t-35">
    <nz-descriptions
      [nzTitle]="titleTemplate"
      nzBordered
      nzSize="small"
      [nzColumn]="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }"
    >
      <ng-template #titleTemplate>
        <span>收货人信息</span>
        
        <button
          nz-button
          nzType="primary"
          nzSize="small"
          class="m-l-10"
          [disabled]="!(permission.userPermission.has('storageCenter:out:examine') && [2, 3, 4].includes(detailsData.state))"
          (click)="showEditReceModal()"
        >修改</button>
      </ng-template>

      <nz-descriptions-item nzTitle="供应商">
        {{ filterText(supplierOptions, detailsData.supId, 'id') }}
      </nz-descriptions-item>
      <nz-descriptions-item nzTitle="收货人">{{ detailsData.consignee || '-' }}</nz-descriptions-item>
      <nz-descriptions-item nzTitle="联系方式">{{ detailsData.tel || '-' }}</nz-descriptions-item>
      <nz-descriptions-item nzTitle="地址">{{ detailsData.address || '-' }}</nz-descriptions-item>
    </nz-descriptions>
  </div>

  <div class="p-t-35">
    <nz-descriptions
      [nzTitle]="titleTemplate2"
      nzBordered
      nzSize="small"
      [nzColumn]="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }"
    >
      <ng-template #titleTemplate2>
        <div>
          <span>配送信息</span>
        
          <button
            nz-button
            nzType="primary"
            nzSize="small"
            class="m-l-10"
            [disabled]="!(permission.userPermission.has('storageCenter:out:examine') && [2, 3, 4].includes(detailsData.state))"
            (click)="showEditDeliveryModal()"
          >修改</button>

          <!-- [disabled]="!([2, 3, 4].includes(detailsData.state) && detailsData.deliveryMethod === 1 && detailsData.expressNo)" -->
          <button
            nz-button
            nzType="primary"
            nzSize="small"
            nzDanger
            class="m-l-10"
            [disabled]="!([2, 3, 4].includes(detailsData.state) && detailsData.deliveryMethod === 1 && detailsData.expressNo)"
            (click)="cancelLogisticsBill()"
          >取消物流单</button>
        </div>
      </ng-template>

      <nz-descriptions-item nzTitle="配送方式">
        <span>
          {{ detailsData.deliveryMethod | storageDeliveryMethod: 'label' }}
        </span>
      </nz-descriptions-item>
      <nz-descriptions-item nzTitle="物流公司">{{ detailsData.expressName || '-' }}</nz-descriptions-item>
      <nz-descriptions-item nzTitle="运费承担">
        {{ detailsData.isFreight === 0 ? '我司承担' : (detailsData.isFreight === 1 ? '用户承担' : '-') }}
      </nz-descriptions-item>
      <nz-descriptions-item nzTitle="物流单号">{{ detailsData.expressNo || '-' }}</nz-descriptions-item>
      <nz-descriptions-item nzTitle="运费">{{ detailsData.expressPrice || '0' }}元</nz-descriptions-item>
    </nz-descriptions>
  </div>
</nz-card>

<nz-card class="m-t-4">
  <p class="detail-title">出库明细</p>

  <div class="table-wrap">
    <nz-table
      #mxTable
      nzBordered
      nzSize="small"
      [nzFrontPagination]="false"
      [nzShowPagination]="false"
      [nzData]="exwarehousTableData"
      (nzCurrentPageDataChange)="onCurrentPageDataChange($event)">
      <thead>
        <tr>
          <th
            rowspan="2"
            nzWidth="50px"
            nzAlign="center"
            [(nzChecked)]="checked"
            [nzIndeterminate]="indeterminate"
            (nzCheckedChange)="onAllChecked($event)"
          ></th>
          <th nzAlign="center" rowspan="2" nzWidth="70px">序号</th>
          <th nzAlign="center" colspan="4">商品信息</th>
          <th nzAlign="center" colspan="4">出库信息</th>
          <th nzAlign="center" rowspan="2">操作</th>
        </tr>
        <tr>
          <th nzAlign="center">商品状态</th>
          <th nzAlign="center">商品名称</th>
          <th nzAlign="center">商品编号</th>
          <th nzAlign="center">商品69码</th>

          <th nzAlign="center">行类型</th>
          <th nzAlign="center">行状态</th>
          <th nzAlign="center">预计出库数量</th>
          <th nzAlign="center">实际出库数量</th>
        </tr>
      </thead>
      <tbody>
        <tr
          *ngFor="let data of mxTable.data; let index = index;"
          [ngClass]="{'tr-bg-red': data?.lineState === 1}">
          <td
            [nzDisabled]="data.lineState === 1"
            [nzChecked]="setOfCheckedId.has(data.id)"
            (nzCheckedChange)="onItemChecked(data.id, $event)"
          ></td>
          <!-- 序号 -->
          <td nzAlign="center">{{ index + 1 }}</td>
          <!-- 商品状态 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : '#67C23A'}">
              {{ data.whGoodsState === 0 ? '禁用' : (data.whGoodsState === 1 ? '启用' : '-') }}
            </span>
          </td>
          <!-- 商品名称 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsName || '-' }}
            </span>
          </td>
          <!-- 商品编号 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsCode || '-' }}
            </span>
          </td>
          <!-- 商品69码 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsTwoCode || '-' }}
            </span>
          </td>
          
          <!-- 行类型 -->
          <td nzAlign="center">
            {{ data.type | storageLineState: 'label' }}
          </td>
          <!-- 行状态 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data?.lineState === 0 ? '#67C23A' : '#F56C6C'}">
              {{ data?.lineState === 0 ? '正常' : (data?.lineState === 1 ? '已作废' : '-') }}
            </span>
          </td>
          <!-- * 预计出库数量 -->
          <td nzAlign="center">
            {{ data?.number || '0' }}
          </td>
          <!-- 实际出库数量 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data?.actualNumber !== data?.number ? '#F56C6C' : ''}">
              {{ data?.actualNumber || '0' }}
            </span>
          </td>
          <!-- 操作 -->
          <td nzAlign="center">
            <ng-container *ngIf="data?.lineState === 0; else elseTemplate">
              <button
                *ngIf="permission.userPermission.has('storageCenter:out:toVoid') && [2, 3, 4].includes(detailsData.state) && data.lineState === 0"
                nz-button
                nzType="link"
                nzSize="small"
                nzDanger
                (click)="showTovoidModal(data)"
              >作废</button>

              <button
                *ngIf="data.itemSns && [0, 1].includes(data.type)"
                nz-button
                nzType="link"
                nzSize="small"
                (click)="showSnInfosModal(data)"
              >{{ data.type === 0 ? '指定SN' : (data.type === 1 ? '已选SN' : '-') }}</button>

              <button
                *ngIf="data.libraryText"
                nz-button
                nzType="link"
                nzSize="small"
                (click)="showLocationModal(data)"
              >多库位</button>
            </ng-container>
            <ng-template #elseTemplate>
              -
            </ng-template>
          </td>
        </tr>

        <tr *ngIf="exwarehousTableData?.length" style="background: #fafafa; font-weight: 600;">
          <td nzAlign="center" colSpan="8">合计</td>
          <!-- 预计出库数 -->
          <td nzAlign="center">{{ calcSumTotal('number') }}</td>
          <!-- 实际出库数 -->
          <td nzAlign="center">{{ calcSumTotal('actualNumber') }}</td>
          <td nzAlign="center">-</td>
        </tr>
      </tbody>
    </nz-table>
  </div>
</nz-card>

<nz-card class="m-t-4">
  <div nz-row nzJustify="center">
    <button
      *ngIf="permission.userPermission.has('storageCenter:out:examine') && [2, 3, 4].includes(detailsData.state)"
      nz-button
      nzType="primary"
      class="m-r-10"
      (click)="subSplitOrder()"
    >拆单</button>

    <button
      nz-button
      nzType="default"
      (click)="pageBack()"
    >返回</button>
  </div>
</nz-card>

<!-- S 作废 Modal -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [(nzVisible)]="isZfVisible"
  [nzTitle]="'提示'"
  (nzOnCancel)="isZfVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <p class="zf-tips">该操作不可逆，作废后该行彻底失效，已出库数量也不会被统计</p>
      
      <nz-form-item>
        <nz-form-label nzRequired>备注</nz-form-label>
        <nz-form-control>
          <nz-textarea-count [nzMaxCharacterCount]="100">
            <textarea
              rows="4"
              nz-input
              maxlength="100"
              placeholder="请输入备注、必填"
              [(ngModel)]="toVoidRemark"
            ></textarea>
          </nz-textarea-count>
        </nz-form-control>
      </nz-form-item>
    </div>

    <div class="custom-model-foot">
      <div class="custom-foot-cont">
        <button nz-button nzType="default" (click)="isZfVisible = false">取消</button>
        <button nz-button nzType="primary" [nzLoading]="submitZfLoading" (click)="submitToVoidData()">确定作废</button>
      </div>
    </div>
  </div>
</nz-modal>
<!-- S 作废 Modal -->

<!-- S SN信息 Modal -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [(nzVisible)]="isSninfoVisible"
  [nzTitle]="'SN信息'"
  (nzOnCancel)="isSninfoVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <div class="p-t-10">
        <nz-table
          #sninfoTable
          nzSize="small"
          nzBordered
          [nzShowPagination]="false"
          [nzData]="snModalTable">
          <thead>
            <tr>
              <th nzAlign="center" nzWidth="100px">序号</th>
              <th nzAlign="center">SN码</th>
            </tr>
          </thead>
          <tbody>
            <tr *ngFor="let data of sninfoTable.data; let index = index;">
              <!-- 序号 -->
              <td nzAlign="center">{{ index + 1 }}</td>
              <!-- SN码 -->
              <td nzAlign="center">{{ data.code || '-' }}</td>
            </tr>
          </tbody>
        </nz-table>
      </div>
    </div>
  </div>
</nz-modal>
<!-- S SN信息 Modal -->

<!-- S 多库位 Modal -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [(nzVisible)]="isDkwVisible"
  [nzTitle]="'多库位'"
  (nzOnCancel)="isDkwVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <div class="p-t-2">
        <nz-table
          #dkwinfoTable
          nzSize="small"
          nzBordered
          [nzShowPagination]="false"
          [nzData]="locationModalTable">
          <thead>
            <tr>
              <th nzAlign="center" nzWidth="100px">序号</th>
              <th nzAlign="center">库位</th>
              <th nzAlign="center">出库数量</th>
            </tr>
          </thead>
          <tbody>
            <tr *ngFor="let data of dkwinfoTable.data; let index = index;">
              <!-- 序号 -->
              <td nzAlign="center">{{ index + 1 }}</td>
              <!-- 库位 -->
              <td nzAlign="center">
                {{ filterText(locationOptions, data.libraryId, 'id') }}
              </td>
              <!-- 出库数量 -->
              <td nzAlign="center">{{ data.num || '0' }}</td>
            </tr>
          </tbody>
        </nz-table>
      </div>
    </div>
  </div>
</nz-modal>
<!-- S 多库位 Modal -->

<!-- S 仓库修改 Modal -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [nzWidth]="560"
  [(nzVisible)]="isStoreVisible"
  [nzTitle]="'修改仓库'"
  (nzOnCancel)="isStoreVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <p class="edit-store-tip p-b-20">
        修改仓库以后，已经填写的出库SN信息等会被清空，该操作不可逆，请注意操作。
      </p>

      <nz-form-item>
        <nz-form-label [nzSpan]="5">现有仓库是</nz-form-label>
        <nz-form-control [nzSpan]="19">
          <b>{{ filterText(warehouseOptions, detailsData.whId, 'id') }}</b>
        </nz-form-control>
      </nz-form-item>

      <nz-form-item>
        <nz-form-label [nzSpan]="5" nzRequired>新仓库</nz-form-label>
        <nz-form-control [nzSpan]="19">
          <nz-select
            nzShowSearch
            nzAllowClear
            nzPlaceHolder="请选择新仓库"
            [(ngModel)]="storeParams.whId">
            <nz-option
              *ngFor="let option of warehouseOptions"
              [nzLabel]="option.name"
              [nzValue]="option.id"
              [nzDisabled]="option.id === detailsData.whId"
            ></nz-option>
          </nz-select>
        </nz-form-control>
      </nz-form-item>
    </div>

    <div class="custom-model-foot">
      <div class="custom-foot-cont">
        <button nz-button nzType="default" (click)="isStoreVisible = false">取消</button>
        <button nz-button nzType="primary" [nzLoading]="modifyStoreLoading" (click)="subModifyStore()">确定</button>
      </div>
    </div>
  </div>
</nz-modal>
<!-- E 仓库修改 Modal -->

<!-- S 修改收货人信息 Modal -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [(nzVisible)]="isReceVisible"
  [nzTitle]="'修改收货人'"
  (nzOnCancel)="isReceVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <form nz-form [formGroup]="validateReceForm">
        <nz-form-item>
          <nz-form-label [nzSpan]="4">供应商</nz-form-label>
          <nz-form-control [nzSpan]="20" nzErrorTip="请选择供应商!">
            <nz-select
              nzShowSearch
              nzAllowClear
              nzPlaceHolder="请选择供应商"
              formControlName="supId">
              <nz-option
                *ngFor="let option of supplierOptions"
                [nzLabel]="option.name"
                [nzValue]="option.id"
              ></nz-option>
            </nz-select>
          </nz-form-control>
        </nz-form-item>

        <nz-form-item>
          <nz-form-label [nzSpan]="4" nzRequired>收货人</nz-form-label>
          <nz-form-control [nzSpan]="20" nzErrorTip="请输入收货人!">
            <input nz-input placeholder="请输入收货人" formControlName="consignee" />
          </nz-form-control>
        </nz-form-item>

        <nz-form-item>
          <nz-form-label [nzSpan]="4" nzRequired>联系方式</nz-form-label>
          <nz-form-control [nzSpan]="20" nzErrorTip="请输入联系方式!">
            <input nz-input placeholder="请输入联系方式" formControlName="tel" />
          </nz-form-control>
        </nz-form-item>

        <nz-form-item>
          <nz-form-label [nzSpan]="4" nzRequired>详细地址</nz-form-label>
          <nz-form-control [nzSpan]="20" nzErrorTip="请输入详细地址!">
            <nz-textarea-count [nzMaxCharacterCount]="200">
              <textarea
                rows="4"
                nz-input
                maxlength="200"
                placeholder="请输入详细地址"
                formControlName="address"
              ></textarea>
            </nz-textarea-count>
          </nz-form-control>
        </nz-form-item>
      </form>
    </div>

    <div class="custom-model-foot">
      <div class="custom-foot-cont">
        <button nz-button nzType="default" (click)="isReceVisible = false">取消</button>
        <button nz-button nzType="primary" [nzLoading]="modifyReceLoading" (click)="subEditReceData()">确定</button>
      </div>
    </div>
  </div>
</nz-modal>
<!-- E 修改收货人信息 Modal -->

<!-- S 修改配送方式 Modal -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [(nzVisible)]="isDeliveryVisible"
  [nzTitle]="'修改配送方式'"
  (nzOnCancel)="isDeliveryVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <form nz-form [formGroup]="validateDeliveryForm">
        <nz-form-item>
          <nz-form-label [nzSpan]="5" nzRequired>运费承担</nz-form-label>
          <nz-form-control [nzSpan]="19" nzErrorTip="请选择运费承担!">
            <nz-radio-group formControlName="isFreight">
              <label nz-radio [nzValue]="0">我方承担</label>
              <label nz-radio [nzValue]="1">用户承担</label>
            </nz-radio-group>
          </nz-form-control>
        </nz-form-item>

        <nz-form-item>
          <nz-form-label [nzSpan]="5" nzRequired>配送方式</nz-form-label>
          <nz-form-control [nzSpan]="19" nzErrorTip="请选择配送方式!">
            <nz-radio-group
              class="radio-psfs-wrap"
              formControlName="deliveryMethod"
              (ngModelChange)="deliveryChange($event)">
              <label
                *ngFor="let option of deliveryOptions"
                nz-radio
                [nzValue]="option.value"
              >{{ option.label }}</label>
            </nz-radio-group>
          </nz-form-control>
        </nz-form-item>

        <nz-form-item>
          <nz-form-label
            [nzSpan]="5"
            [nzRequired]="[1, 2].includes(validateDeliveryForm.value.deliveryMethod)">
            物流公司
          </nz-form-label>
          <nz-form-control [nzSpan]="19" nzErrorTip="请选择物流公司!">
            <nz-select
              nzShowSearch
              nzAllowClear
              nzPlaceHolder="请选择物流公司"
              [nzDisabled]="[3].includes(validateDeliveryForm.value.deliveryMethod)"
              formControlName="expressCode">
              <nz-option
                *ngFor="let option of logisticsOptions"
                [nzDisabled]="!['JD', 'SF'].includes(option.content) && validateDeliveryForm.value.deliveryMethod == 1"
                [nzLabel]="option.name"
                [nzValue]="option.content"
              ></nz-option>
            </nz-select>
          </nz-form-control>
        </nz-form-item>

        <nz-form-item>
          <nz-form-label
            [nzSpan]="5"
            [nzRequired]="[2].includes(validateDeliveryForm.value.deliveryMethod)">
            物流单号
          </nz-form-label>
          <nz-form-control [nzSpan]="19" nzErrorTip="请输入物流单号!">
            <input
              nz-input
              placeholder="请输入物流单号"
              formControlName="expressNo"
            />
          </nz-form-control>
        </nz-form-item>

        <nz-form-item>
          <nz-form-label [nzSpan]="5">运费</nz-form-label>
          <nz-form-control [nzSpan]="19" nzErrorTip="请输入运费!">
            <nz-input-number
              style="width: 50%;"
              [nzMin]="0"
              [nzMax]="99999"
              [nzStep]="1"
              [nzDisabled]="[1].includes(validateDeliveryForm.value.deliveryMethod)"
              nzPlaceHolder="请输入运费"
              formControlName="expressPrice"
            ></nz-input-number>
          </nz-form-control>
        </nz-form-item>
      </form>
    </div>

    <div class="custom-model-foot">
      <div class="custom-foot-cont">
        <button nz-button nzType="default" (click)="isDeliveryVisible = false">取消</button>
        <button nz-button nzType="primary" [nzLoading]="modifyDeliveryLoading" (click)="subEditDeliveryData()">确定</button>
      </div>
    </div>
  </div>
</nz-modal>
<!-- E 修改配送方式 Modal -->